<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 无限极分类
function infinite($list,$parent_id=0,$deep=0){
    static $arr = array();
    foreach ($list as $v){
        if($v['p_id']==$parent_id){
            $v['deep'] = $deep;
            $arr[] = $v;
            infinite($list,$v['id'],$deep+1);
        }
    }
    return $arr;
}

/**
 * 搜索调用
 * @param array $data 查询条件
 * @param string $time 时间字段名
 * @param string $name 名称字段名,多个字段用逗号隔开
 * @param string $status 状态
 * @return array
 * */
function search($data,$time = '',$name = ''){
    $condition = 1;
    $display   = array();
    // 根据时间查询
    if(!empty($data['startTime'])){
        $startTime  = strtotime($data['startTime']);
        $condition .= " and $time > {$startTime}";
        $display['startTime'] = $data['startTime'];
    }
    if(!empty($data['endTime'])){
        $endTime    = strtotime(date('Y-m-d',strtotime($data['endTime'].'+1 day')));
        $condition .= " and $time < {$endTime}";
        $display['endTime'] = $data['endTime'];
    }
    if(!empty($data['startTime']) && !empty($data['endTime'])){
        $startTime  = strtotime($data['startTime']);
        $endTime    = strtotime(date('Y-m-d',strtotime($data['endTime'].'+1 day'))) - 1;
        $condition .= " and $time between {$startTime} and {$endTime}";
        $display['startTime'] = $data['startTime'];
        $display['endTime']   = $data['endTime'];
    }
    // 根据名称查询
    if(!empty($data['searchName'])){
        $arr = explode(',',$name);
        $condition .= ' and ( 0';
        foreach($arr as $v){
            $condition .= " or $v like '%{$data['searchName']}%'";
        }
        $condition .= ')';
        $display['searchName'] = $data['searchName'];
    }
    return array('condition' => $condition,'display' => $display);

}
/**
 *单张图片删除
 */
function removeImg($path,$image){
    $arr = explode('/',$path);
    $leng = count($arr);
    $upPath = $arr[$leng-3];
    $dirPath = $arr[$leng-2];
    $root = $_SERVER['DOCUMENT_ROOT'];
    $url = $root.'/'.$upPath.'/'.$dirPath.'/'.$image;
    return unlink($url);
}
/**
 * @return bool
 * 验证用户是否登录
 */
function checkLogin(){
    $uid = cookie('uid');
    if(!empty($uid)){
        return $uid;
    }
    return false;
}
function uploadFile(){

}
/* 导出excel函数*/
 function push($data){
     $params= [
         'ID' => 'ID',
         'username' => '账号',
         'openid' => '账号绑定',
         'nickname' => '昵称',
         'real_name' => '真实姓名',
         'type' => '类型',
         'name' => '产品',
         'expiration_time' => '当前有效期',
         'integral' => '积分',
         'source' => '注册来源',
         'tixin' => '短信提醒',
         'last_login_time' => '最后登录时间',
         'create_time' => '注册时间',
         'status' => '状态',
     ];
     array_unshift($data,$params);
     $objPHPExcel = new \PHPExcel();
     //接下来就是写数据到表格里面去
         foreach ($data as $key => $value) {
             $i=$key+1;//表格是从1开始的
             $objPHPExcel->getActiveSheet()->setCellValue('A'.$i,  $value['ID']);//这里是设置A1单元格的内容
             $objPHPExcel->getActiveSheet()->setCellValue('B'.$i,  $value['username']);////这里是设置B1单元格的内容
             $objPHPExcel->getActiveSheet()->setCellValue('C'.$i,  $value['openid']);
             $objPHPExcel->getActiveSheet()->setCellValue('D'.$i,  $value['nickname']);
             $objPHPExcel->getActiveSheet()->setCellValue('E'.$i,  $value['real_name']);
             $objPHPExcel->getActiveSheet()->setCellValue('F'.$i,  $value['type']);
             $objPHPExcel->getActiveSheet()->setCellValue('G'.$i,  $value['name']);
             $objPHPExcel->getActiveSheet()->setCellValue('H'.$i,  $value['expiration_time']);
             $objPHPExcel->getActiveSheet()->setCellValue('I'.$i,  $value['integral']);
             $objPHPExcel->getActiveSheet()->setCellValue('J'.$i,  $value['source']);
             $objPHPExcel->getActiveSheet()->setCellValue('K'.$i,  $value['tixin']);
             $objPHPExcel->getActiveSheet()->setCellValue('L'.$i,  $value['last_login_time']);
             $objPHPExcel->getActiveSheet()->setCellValue('M'.$i,  $value['create_time']);
             $objPHPExcel->getActiveSheet()->setCellValue('N'.$i,  $value['status']);
             $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(14);
             $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(14);
             $objPHPExcel->getActiveSheet()->getColumnDimension('M')->setWidth(20);
             $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(20);
             $objPHPExcel->getActiveSheet()->getColumnDimension('L')->setWidth(20);
             $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(16);

         }

     $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);//设置保存版本格式
     //接下来当然是下载这个表格了，在浏览器输出就好了
     header("Pragma: public");
     header("Expires: 0");
     header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
     header("Content-Type:application/force-download");
     header("Content-Type:application/vnd.ms-execl");
     header("Content-Type:application/octet-stream");
     header("Content-Type:application/download");;
     header('Content-Disposition:attachment;filename=文件名称.xls');
     header("Content-Transfer-Encoding:binary");
     $objWriter->save('文件名称.xls');
     $objWriter->save('php://output');
}